<html>
<!-- Copyright 2010 andry.virvich@gmail.com. All Rights Reserved. -->
  <head>
	<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
	<script type="text/javascript" src="js/jquery.xml.min.js"></script>
	<script type="text/javascript" src="js/jquery.json-2.2.min.js"></script>
    <script type="text/javascript" src="js/chrome_ex_oauthsimple.js"></script>
    <script type="text/javascript" src="js/chrome_ex_oauth.js"></script>
    <script type="text/javascript" src="js/xsddate.js"></script>
	<script>

var loader= {
	tabs: [],
	projectsData: [],
	oauth: null,
	init: function(oauth){
		try{
			loader.oauth = oauth;
			var sOpt = localStorage.getItem('projects');
			var opt;
			if(sOpt && sOpt != ''){
				try{
					loader.tabs = $.evalJSON(sOpt);
					if(typeof(loader.tabs[0]) == 'string')
						loader.convert();
					if(typeof(loader.tabs[0].columns) == 'undefined'){
						$.map(loader.tabs, function(item, i){
							loader.tabs[i].columns = [true,true,true,true,true,true,false,false,false];
						});
						localStorage.setItem('projects',$.toJSON(loader.tabs));
					} else {
						$.map(loader.tabs, function(item, i){
							for(j=1; j<=10; j++){
								if(typeof(loader.tabs[i].columns[j]) == 'undefined')
									loader.tabs[i].columns[j] = false;
							}
						});
						localStorage.setItem('projects',$.toJSON(loader.tabs));
					}
				}catch(e){}
			}
			loader.update();
		}catch(e){
			alert(e);
		}
		
		var updateInterval =  (localStorage.getItem('updateinterval'))?localStorage.getItem('updateinterval'):60000;
		window.setInterval(loader.update, updateInterval);
	},
	update: function(){
		$.map(loader.tabs, function(item, i){
			loader.getIssuesForProject(item, i);
		});
	},
	getIssuesForProject: function(project, i){
		loader.oauth.sendSignedRequest("http://code.google.com/feeds/issues/p/"+project.projectName+"/issues/full", function(xml, xhr){
				if(i == 0){
					chrome.browserAction.setBadgeText({text: $("entry", xhr.responseXML).size().toString()});
				}
				loader.projectsData[i] = loader.parseXml(xhr.responseXML);
		}, {
			"parameters": {
				"can": project.projectFilter,
				"q": project.projectQuery
			}
		});
	},
	parseXml: function(xhr){
		var data = [];
		$(xhr).find("entry").each(function(i, val) {
			try{
					var labels = {"Priority":'',"Type":''};
					$("label", val).each(function(i, label){
						var txt = $(label).text();					
						//alert(txt);
						for(j in labels){
							if(txt.indexOf(j)==0)
								labels[j] = txt.replace(j+"-", "");
						}
					});
					
					data.push([
						$("link[rel*='alternate']", val).attr("href"),
						$("id", val).eq(1).text(),
						$("state", val).text(),
						$("status", val).text(),
						$("name", val).text(),
						$("username", val).text(),
						$("title", val).text(),
						$("stars", val).text(),
						loader.getDate($("published", val).text()),
						loader.getDate($("updated", val).text()),
						labels.Priority,
						labels.Type
					]);
			} catch(e){
				alert(e);
			}
		});
		return data;
	},
	getDate: function(str){
		if(str!='' && typeof(str)!="undefined"){
			try{
				var d= parseXSDDateString(str);
				return (d.getMonth()+1) + "." +d.getDate() + "." + d.getFullYear();
			}catch(e){
				return '';
			}
		} else {
			return '';
		}
	},
	convert: function(){
		$.map(loader.tabs, function(item, i){
			loader.tabs[i] = {
				projectName: loader.tabs[i],
				projectFilter: "all"
			}
		});
		localStorage.setItem('projects',$.toJSON(loader.tabs));
	}
}
	var oauth = ChromeExOAuth.initBackgroundPage({
		'request_url' : 'https://www.google.com/accounts/OAuthGetRequestToken',
		'authorize_url' : 'https://www.google.com/accounts/OAuthAuthorizeToken',
		'access_url' : 'https://www.google.com/accounts/OAuthGetAccessToken',
		'consumer_key' : 'anonymous',
		'consumer_secret' : 'anonymous',
		'scope' : "http://code.google.com/feeds/issues/",
		'app_name' : 'Google Code Issue tracker for Chrome'
	});
	
	function Auth(){
		oauth.authorize(function() {
			chrome.browserAction.setIcon({ 'path' : 'images/16x16.png'});
			//chrome.tabs({ url: chrome.extension.getURL('options.html'), selected: true });
			loader.init(oauth);
		});
	}
	function logout() {
		try{
			oauth.clearTokens();
			loader.oauth.clearTokens();
			chrome.browserAction.setIcon({ 'path' : 'images/16x16off.png'});
		}catch(e){}
	};
	
	function chromeExOAuthOnAuthorize(token, secret){
		chrome.tabs({ url: chrome.extension.getURL('options.html'), selected: true });
	}
	Auth();
	</script>
 </head>
  <body>
  </body>
</html> 